/*
 * TimeStamp.java
 * Created on Apr 12, 2005 12:21:58 PM
 */

/**
 * @author Andry
 * The TimeStamp.java class provides
 *
 */
package edu.mobbuzz.messaging;
public class TimeStamp {
    public int year;
    public int month;
    public int day;
    public int hour;
    public int minute;
    public int second;
    public int timezone;
    
    public TimeStamp(int year, int month, int day, int hour, 
            int minute, int second, int timezone) {
        this.year 	= year;
        this.month 	= month;
        this.day 	= day;
        this.hour	= hour;
        this.minute	= minute;
        this.second = second;
        this.timezone = timezone;
    }
    
    /**
     * Parse string of time (GMT) with format YYYYMMDD'T'hh:mm:ss and return as TimeStamp
     * @param timeString		String of time with format YYYYMMDD'T'hh:mm:ss (GMT)
     * @param gmt_timezone		GMT timezone hour difference
     * @return					TimeStamp object of input string or null if invalid string
     */
    public static TimeStamp parseTimeString(String timeString, int gmt_timezone) {
        if (timeString.length() == 17) {
            int year 	= Integer.parseInt(timeString.substring(0, 4));
            int month 	= Integer.parseInt(timeString.substring(4, 6));
            int day		= Integer.parseInt(timeString.substring(6, 8));
            int hour	= Integer.parseInt(timeString.substring(9, 11));
            int minute	= Integer.parseInt(timeString.substring(12, 14));
            int second	= Integer.parseInt(timeString.substring(15, 17));
            return (new TimeStamp(year, month, day, hour, minute, second, gmt_timezone));
        } else {
            return null;
        }
    }
    
    /**
     * Parse string of time with format YYYYMMDDhhmmss and return as TimeStamp
     * @param timeString		String of time with format YYYYMMDDhhmmss (GMT)
     * @param gmt_timezone		GMT timezone hour difference
     * @return					TimeStamp object of input string or null if invalid string
     */
    public static TimeStamp parseTimeStringUnformatted(String timeString, int gmt_timezone) {
        if (timeString.length() == 14) {
            int year 	= Integer.parseInt(timeString.substring(0, 4));
            int month 	= Integer.parseInt(timeString.substring(4, 6));
            int day		= Integer.parseInt(timeString.substring(6, 8));
            int hour	= Integer.parseInt(timeString.substring(8, 10));
            int minute	= Integer.parseInt(timeString.substring(10, 12));
            int second	= Integer.parseInt(timeString.substring(12, 14));
            return (new TimeStamp(year, month, day, hour, minute, second, gmt_timezone));
        } else {
            return null;
        }
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return ((new TimeStamp(year, month, day, hour, minute, second, 0).toLocalString()));
    }

    
   /**
     * Get this timestamp in string with format DD/MM/YYYY hh:mm in local timezone
     * @return String DD/MM/YYYY hh:mm in local timezone
     */
    public String toLocalString() {
        TimeStamp time = normalizeTime(new TimeStamp(year, month, day, hour + timezone, minute, second, 0));
        StringBuffer output = new StringBuffer();
        if (time.day < 10) {
            output.append('0');
        }
        output.append(time.day).append("/");
        if (time.month < 10) {
            output.append('0');
        }
        output.append(time.month).append("/").append(time.year).append(" ");
        if (time.hour < 10) {
            output.append('0');
        }
        output.append(time.hour).append(':');
        if (time.minute < 10) {
            output.append('0');
        }
        return output.append(time.minute).toString();
    }
    
    /**
     * Get this timestamp in string with format YYYYMMDD'T'hh:mm:ss int GMT+0 timezone
     * @return	String in format YYYYMMDD'T'hh:mm:ss in GMT+0 timezone
     */
/*    public String toStringInternetTimeFormat() {
        StringBuffer output = new StringBuffer(Integer.toString(year));
        if (month < 10) {
            output.append('0');
        }
        output.append(month);
        if (day < 10) {
            output.append('0');
        }
        output.append(day).append('T');
        if (hour < 10) {
            output.append('0');
        }
        output.append(hour).append(':');
        if (minute < 10) {
            output.append('0');
        }
        output.append(minute).append(':');
        if (second < 10) {
            output.append('0');
        }
        return output.append(second).toString();
    }*/
    
    /**
     * Get this timestamp in string with format YYYYMMDDhhmmss in local timezone
     * @return String YYYYMMDDhhmm in local timezone
     */
    private String toLocalStringUnformatted() {
        TimeStamp time = normalizeTime(new TimeStamp(year, month, day, hour + timezone, minute, second, 0));
        StringBuffer output = new StringBuffer(Integer.toString(time.year));
        if (time.month < 10) {
            output.append('0');
        }
        output.append(time.month);        
        if (time.day < 10) {
            output.append('0');
        }
        output.append(time.day);
        if (time.hour < 10) {
            output.append('0');
        }
        output.append(time.hour);
        if (time.minute < 10) {
            output.append('0');
        }
        output.append(time.minute);
        if (time.second < 10) {
            output.append('0');
        }
        return output.append(time.second).toString();
    }
    
    /**
     * Get this timestamp in string with format YYYYMMDDhhmmss in GMT timezone
     * @return String YYYYMMDDhhmm in GMT timezone
     */
    public String toStringUnformatted() {
        return ((new TimeStamp(year, month, day, hour, minute, second, 0)).toLocalStringUnformatted());
    }
    
    /**
     * Normalize TimeStamp which have hour attribute more than 23 or less than 0
     * @param time	TimeStamp to be normalized
     * @return		Normalized TimeStamp
     */
    public static TimeStamp normalizeTime(TimeStamp time) {
        int day = time.day;
        int month = time.month;
        int year = time.year;
        int hour = time.hour;
        if (hour > 23) {
            hour -= 24;
            if (day == 31) {
            	day = 1;
                if (month == 12) {
                    month = 1;
                    year++;
                } else {                    
                    month++;
                }
            } else if (day == 30) {
                if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
                    day = 1;
                    month++;
                } else {
                    day = 31;
                }
            } else if ((day == 28) && (month == 2)) {
                if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) {
                    day = 29;
                } else {
                    day = 1;
                    month = 3;
                }
            } else if ((day == 29) && (month == 2)) {
                day = 1;
                month = 3;
            } else {
                day++;
            }
            return (new TimeStamp(year, month, day, hour, time.minute, time.second, time.timezone));
        } else if (hour < 0) {
            hour += 24;
            if (day == 1) {
                if (month > 1) {
                    month--;
	                if ((month == 1) || (month == 3) || (month == 5) || (month == 7) ||
	                            (month == 8) || (month == 10)) {
	                        day = 31;
                    } else if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
                        day = 30;
                    } else {	// month == 2
                        if ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) { // Leap year
                            day = 29;
                        } else {
                            day = 28;
                        }
                    }
                } else {
                    day = 31;
                    month = 12;
                    year--;
                }
            } else {
                day--;
            }
            return (new TimeStamp(year, month, day, hour, time.minute, time.second, time.timezone));
        } else {
            return time;
        }
    }
}
